﻿using System;
using Dimebrain.TweetSharp.Core.OAuth;
using Dimebrain.TweetSharp.Extensions;
using Dimebrain.TweetSharp.Fluent;
using Dimebrain.TweetSharp.Model;

namespace TestTwitterLibrary
{
    internal class Program
    {
        private static void Main()
        {
            // get these from app.config
            var consumerKey = Settings.Default.ConsumerKey;
            var consumerSecret = Settings.Default.ConsumerSecret;

            // get an authenticated request token from twitter
            var requestToken = GetRequestToken(consumerKey, consumerSecret);

            // automatically starts the default web browser, sending the 
            // user to the authorization URL.
            FluentTwitter.CreateRequest()
                .Authentication
                .AuthorizeDesktop(consumerKey,
                                  consumerSecret,
                                  requestToken.Token);

            // user authorization occurs out of band, so wait here
            Console.WriteLine("Input the PIN granted by Twitter then press enter.");
            string sPin = Console.ReadLine();

            // exchange the unauthenticated request token with an authenticated access token,
            // and remember to persist this authentication pair for future use
            var accessToken = GetAccessToken(consumerKey, consumerSecret, requestToken.Token,sPin);

            //// make an authenticated call to Twitter with the token and secret
            //var verify = FluentTwitter.CreateRequest()
            //    .AuthenticateWith(consumerKey, consumerSecret, accessToken.Token, accessToken.TokenSecret)
            //    .Account().VerifyCredentials().AsJson();

            //var response = verify.Request();
            //GetResponse(response);

            //setup the message to be sent to the account
            string sTestMessage = "This is a test message from my app message rotator!";

            //Send that message
            var request1 = FluentTwitter.CreateRequest()
                .AuthenticateWith(consumerKey, consumerSecret, accessToken.Token, accessToken.TokenSecret).Statuses().Update(sTestMessage);

            var response = request1.Request();

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }

        private static void GetResponse(string response)
        {
            var identity = response.AsUser();
            if (identity != null)
            {
                Console.WriteLine("{0} authenticated successfully.", identity.ScreenName);
            }
            else
            {
                var error = response.AsError();
                if (error != null)
                {
                    Console.WriteLine(error.ErrorMessage);
                }
            }
        }

        private static OAuthToken GetRequestToken(string consumerKey, string consumerSecret)
        {
            var requestToken = FluentTwitter.CreateRequest()
                .Authentication.GetRequestToken(consumerKey, consumerSecret);

            var response = requestToken.Request();
            var result = response.AsToken();

            if (result == null)
            {
                var error = response.AsError();
                if (error != null)
                {
                    throw new Exception(error.ErrorMessage);
                }
            }

            return result;
        }

        private static OAuthToken GetAccessToken(string consumerKey, string consumerSecret, string token,string sPin)
        {
            var accessToken = FluentTwitter.CreateRequest()
                .Authentication.GetAccessToken(consumerKey, consumerSecret, token,sPin);

            var response = accessToken.Request();
            var result = response.AsToken();

            if (result == null)
            {
                var error = response.AsError();
                if (error != null)
                {
                    throw new Exception(error.ErrorMessage);
                }
            }

            return result;
        }
    }
}